<?php
/**
 * @Author: kendy
 * @Date:   2020-03-17 15:24:25
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2020-04-20 09:19:25
 */
namespace app\hotel\model;

use think\Model;
use think\Db;
use think\Session;
use think\Request;
use app\common\model\UserModel as um;
use app\common\model\OrderRefund as orf;
class OrderRefunds extends Model
{
/** 
	 * @Author   kendy
	 * @DateTime 2019-12-19T10:54:09+0800
	 * @return   [type]                   [description]
	 */
  public function getOrderRefundList($where,$Iscount = false){

		$shopId = (int)Session::get('hotel_id');   
		$where['oc.object_id'] = $shopId;
		$where['oc.object_type'] = 4;
		$where['ho.is_del'] = 0;
		$rs = Db::name("order_refund")->alias('oc') 
		           ->field('oc.refund_id,oc.object_type,ho.order_id,oc.refund_status,oc.create_time,oc.refund_reson,u.nickname,ho.tel,ho.linkman,oc.order_sn,back_money')
				   ->join('__USER__ u','oc.u_id=u.uid','left')
				   ->join('__HOTEL_ORDER__ ho','oc.order_sn=ho.order_sn')
				   ->where($where)
				   ->order('oc.refund_id desc')
				   ->select();
		if(count($rs)>0)
		{
			if($Iscount)
				return count($rs);  
			foreach ($rs as $key => $value) {
			 	 $rs[$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
			 } 
			return $rs;
		}
		return $rs;
		 
	}
	 /**
     * [getOrderInfoById 根据酒店id获取酒店详情]
     * @Author   kendy
     * @DateTime 2019-12-23T11:08:42+0800
     * @return   [type]                   [description]
     */
     static function getOrderInfoById($where)
     {
        

           $resultData = Db::name('hotel_order')
            ->alias('ho')
            ->join('hotel h','ho.hotel_id=h.hotel_id','left')
            ->join('hotel_room hr','hr.room_id=ho.room_id','left')
            ->where($where)
            ->field('h.hotel_name,hr.room_name,h.hotel_tel,ho.order_id,ho.order_sn,ho.hotel_id,ho.room_id,ho.uid,ho.linkman,ho.tel,ho.start_time,
            ho.end_time,ho.living_days,ho.living_peason,ho.content,ho.order_status,ho.total_price,ho.coupon_id,
            ho.coupon_price,ho.order_price,ho.pay_type,ho.is_pay,ho.cancel_id,ho.is_appraise,ho.is_refund,ho.order_unique,ho.is_del,ho.create_time')
            ->find();
        if(empty($resultData)) return [];
        $nameObj = Db::name('hotel_orderlist')->where($where)->select();
        foreach ($nameObj as $k => $v) {
          $nameArr[] = $v['username'];
        }
       // $resultData['linkman'] = implode(' , ', $nameArr);
        $resultData['pay_type'] = lvPayType($resultData['pay_type']);
        $resultData['order_status'] = lvOrderStatus($resultData['order_status']);
        $resultData['create_time'] = date('Y-m-d H:i:s',$resultData['create_time']);
        $resultData['start_time'] = date('Y-m-d',$resultData['start_time']);
        $resultData['end_time'] = date('Y-m-d',$resultData['end_time']);
        $userInfo = new um;
        $resultData['username'] = $userInfo->getUserInfoById(['uid'=>$resultData['uid']])['nickname'];
        return $resultData;
     }

     public function refundorder(){
     	 $isAffirm = (int)input('post.is_affirm');
        $orderSn = input('post.order_sn');  
        $refundId = input('post.refund_id');  
        Db::startTrans(); 
        try{
            $order = Db::name("hotel_order")
            ->field("order_price,tel,order_sn,order_id,uid,pay_type,order_status,order_sn")
            ->where('order_sn',$orderSn)
            ->find();
            if($order['order_status'] != 1){
                 return ['code'=>0,'msg'=>'','count'=>1,'data'=>"无效的退款订单"];  
            }
            $refundInfo = Db::name('order_refund')
            ->where('refund_id',$refundId)
            ->find();
            if($refundInfo['refund_status'] != 0){
            	return ['code'=>0,'msg'=>'','count'=>1,'data'=>"该退款订单已处理"];  
            }
            //修改退款单信息 
            $refund['refund_time'] = time();
            $refund['refund_status'] = $isAffirm;
            Db::name('order_refund')
            ->where('refund_id',$refundId)
            ->update($refund);
           
            //是否同意退款
            if($isAffirm == 1){
                 //修改订单状态
                $order['is_refund'] = 1;
                $order['order_status'] = 5;
                Db::name('hotel_order')
                ->where('order_sn',$orderSn)
                ->update($order);
                $orf = new orf();
                 //退款接口
                //查看支付方式
                $payType = $order['pay_type'];//1支付宝 2微信  
                if($payType == 1){
                   $order['refund_reason'] = "酒店用户申请退款"; 
                   $rs = $orf->aliRefund($order);
                }
                if($payType == 2){ 
                    $order['refund_reason'] = "酒店用户申请退款"; 
                    $rs = $orf->weixinRefund($order);
                } 
                if($rs == 'ERROR'){
                 return ['code'=>0,'msg'=>'','data'=>'退款失败'];  
                }
                  //发送一条用户信息  
                 $sends = [
                    '1'=>$order['order_sn'],
                    '2'=> $order['order_price']
                   ];  
                sendJsmsMessage($order['tel'],174983,$sends);
                   //给用户发送消息提示 1景点订单 2酒店订单 3旅行社订单
                   send_user_message(6, 2, $order['uid'], $orderSn,'','',2);
                }else{
                	//发送一条用户信息 
                   //给用户发送消息提示 1景点订单 2酒店订单 3旅行社订单
                   send_user_message(7, 2, $order['uid'], $orderSn,'','',2);
                }
            
           
            Db::commit();
            return ['code'=>1,'msg'=>'','data'=>"操作成功"];  
        }catch (\Exception $e) {
            Db::rollback();
        }
       return ['code'=>0,'msg'=>'','data'=>$e->getMessage()];  
     }
}